<?php




namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Models\MarketLevel;
use App\Models\User\Profile;
use App\Models\User\User;
use App\Models\UserInfo;
use App\Repositories\ResponseCode;
use App\Services\Validate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Jiannei\Response\Laravel\Support\Facades\Response;

class BasicController extends Controller
{

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {
//        $language = $this->request->header('accept-language');
//        $language = 'zh-CN';
//        App::setLocale($language);
        global $_G;
        $_G['uid']=$this->uid();
//        $_G['level']=(new MarketLevel())->getNowLevel($_G['uid']);
    }


    /**
     * UID
     * @return int
     */
    protected function uid()
    {
        $referer = $this->request->header('referer');
        $token = $this->request->header('Authorization');
        if (strstr($referer, 'apidoc') && $token == 'Bearer') return 1;
        return Auth::id();
    }

    /**
     * @return mixed
     */
    protected function sid()
    {
        return UserInfo::where('uid', self::uid())->value('shop_id');
    }


    /**
     * 获取数据
     * @param $fields
     * @return array
     */
    protected function data(array $fields)
    {
        $input = $this->request->only($fields);
        return (new Validate())->check($input, $fields);
    }

    /**
     * 获取数据
     * @param $field
     * @return mixed
     */
    protected function field($field)
    {
        if ($this->request->filled($field)) {
            return $this->request->input($field);
        } else {
            Response::fail(__('api.MISS_PARAM'), ResponseCode::MISS_PARAM);
        }
    }

}
